<?php
namespace app\api\controller\phone;
class Variable
{
    const environment = true;
    const  formal = 'https://openapi.gongmall.com'; //正式环境地址
    const  test = 'https://openapi-qa.gongmall.com';//测试环境地址
    const  APP_KEY = "appKey"; //app_key
    const  APP_SECRET = "appSecret"; //密钥
    const  NONCE = "nonce";  //随机数
    const  TIMESTAMP = "timestamp"; //当前毫秒时间戳
    const  SERVICE_ID = "serviceId";
    const  SIGN = "sign";

    //获取签名
    public static function getSign($paramMap, $appSecret){
        $text = self::getUrlText($paramMap);
        $text .= "&appSecret=" . $appSecret;
        return strtoupper(md5($text));
    }
    private static function getUrlText($paramMap)
    {
        $beanMap = self::getSortedMap($paramMap);
        $builder = "";
        foreach ($beanMap as $key => $value) {
            if (is_string($value)) {
                $value = $value;
            } else {
                $value = json_encode($value, JSON_UNESCAPED_UNICODE);
            }
            $builder .= $key . '=' . $value . '&';
        }
        $text = $builder;
        return substr($text, 0, -1);
    }
    //对普通map进行排序
    private static function getSortedMap($paramMap)
    {
        $map = [];
        foreach ($paramMap as $key => $value) {
            if ($key !== null && $key !== self::APP_SECRET) {
                $valueStr = strval($value);
                if ($valueStr !== null && $valueStr !== "") {
                    $map[$key] = $value;
                }
            }
        }
        ksort($map);
        return $map;
    }
    public function main($url,$paramMap)
    {
        if(self::environment){
            $http = self::test;
        }else{
            $http = self::formal;
        }
        $allUrl = $http.$url;
        $appKey = "36a7a582e90441819263d11b9f001e23";
        $appSecret = "a36ecd05e7cedc6125f86c8fb9a3811c";
        $nonce = $this->generateRandomString();
        $paramMap[self::APP_KEY] = $appKey;
        $paramMap[self::TIMESTAMP] = time() * 1000;
        $paramMap[self::NONCE] = $nonce;
        $sign = self::getSign($paramMap, $appSecret);
        $paramMap[self::SIGN] = $sign;
        return $this->doPost($allUrl, $paramMap);
    }
    public function generateRandomString($length=24) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        $max = strlen($characters) - 1;

        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $max)];
        }

        return $randomString;
    }
    public function doPost($url,$paramMap){
        $ch = curl_init($url);
        $postFields = [];
        foreach ($paramMap as $key => $value) {
            $postFields[] = urlencode($key) . '=' . urlencode($value);
        }
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $postFields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'cURL error: ' . curl_error($ch);
        }
        curl_close($ch);
        return json_decode($response);
    }

}